﻿$(function (e) {
    $('#bonusScore').css('font-size', '20px');
    
    var reentrance = false;
    $('[data-rel]').unbind("click").click(onPlusClick);

    function onPlusClick() {
        if (reentrance) return false;
        reentrance = true;
        var data = {};
        data.personId = $(this).attr('personId');
        data.url = $(this).attr('data-url');
        $.ajax({
            url: data.url,
            type: 'POST',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify(data),
        }).done(function (res) {
            reentrance = false;
            refresh(res);
        });

        return false; // supposed to fix double events; calls stopPropagation and preventDefault
    };

    function refresh(game) {
        $('#bonusScore').html("Бонус: " + game.BonusScore);
        for (var i = 0; i < game.Players.length; i++) {
            var player = game.Players[i];
            var label = $('#' + player.Id);
            if (label != undefined)
                label.html(player.PlayerScore);
        }
    }

});